/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_hdi_test_entry.h"
#include "sle_adapter_hdi.h"
#include "sle_host_hdi.h"
#include "sle_unit_test_log.h"

SleHostHdiOpts* g_sleHostHdiUtOpts = NULL;
SleHostHdiCallbacks *g_sleHostHdiUtCbs = NULL;


void OnSleHdiUtEnabled(SleHdiErrorCode errCode)
{
    SLE_UT_LOG("[SleHostHdiTest][PASSED] OnSleHdiUtEnabled(%08X)", errCode);
}

void OnSleHdiUtDisabled(SleHdiErrorCode errCode)
{
    SLE_UT_LOG("[SleHostHdiTest][PASSED] OnSleHdiUtDisabled(%08X)", errCode);
}

void GetSleHostHdiOptsTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = GetSleHostHdiOpts(g_sleHostHdiUtOpts);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] GetSleHostHdiOptsTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] GetSleHostHdiOptsTestLevel0");
}

void SleRegisterHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    g_sleHostHdiUtCbs->OnSleEnabledCb = OnSleHdiUtEnabled;
    g_sleHostHdiUtCbs->OnSleDisabledCb = OnSleHdiUtDisabled;
    ret = g_sleHostHdiUtOpts->registerHostCbks(g_sleHostHdiUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleRegisterHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleRegisterHdiTestLevel0 pass");
}

void SleEnableSleHostHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_sleHostHdiUtOpts->enableSle();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleEnableSleHostHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleEnableSleHostHdiTestLevel0 pass");
}

void SleGetHostAddressHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress addr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .addr = {0}
    };
    ret = g_sleHostHdiUtOpts->getHostAddress(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleGetHostAddressHdiTestLevel0 failed1(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleGetHostAddressHdiTestLevel0 pass1");


    addr.addrType = SLE_RANDOM_ADDRESS_TYPE;
    ret = g_sleHostHdiUtOpts->getHostAddress(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleGetHostAddressHdiTestLevel0 failed2(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleGetHostAddressHdiTestLevel0 pass2");
}

uint8_t hostName[SLE_DEVICE_NAME_LEN] = {'h', 'd', 'i', 'U', 'T', 'h', 'o', 's', 't', 'n', 'a', 'm', 'e', '\0'};
void SleSetHostNameHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_sleHostHdiUtOpts->setHostName(hostName, SLE_DEVICE_NAME_LEN);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleSetHostNameHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleSetHostNameHdiTestLevel0 pass");
}

void SleGetHostNameHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    uint8_t name[SLE_DEVICE_NAME_LEN] = {0};
    uint8_t len = SLE_DEVICE_NAME_LEN;
    ret = g_sleHostHdiUtOpts->getHostName(name, &len);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleGetHostNameHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleGetHostNameHdiTestLevel0 pass");
}

void SleDisableSleHostHdiTestLevel0(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_sleHostHdiUtOpts->disableSle();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleDisableSleHostHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleDisableSleHostHdiTestLevel0 pass");
}

void SleHostHdiTestLevel0(void)
{
    GetSleHostHdiOptsTestLevel0();
    SleRegisterHdiTestLevel0();
    SleEnableSleHostHdiTestLevel0();
    sleep(1);
    SleGetHostAddressHdiTestLevel0();
    SleSetHostNameHdiTestLevel0();
    SleGetHostNameHdiTestLevel0();
    SleDisableSleHostHdiTestLevel0();
    sleep(1);
}

void SleEnableSleHostHdiTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_sleHostHdiUtOpts->enableSle();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleEnableSleHostHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleEnableSleHostHdiTestLevel1 pass");
}

void SleGetHostAddressHdiTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    SleDeviceAddress addr = {
        .addrType = 255
    };
    //测试入参为NULL
    ret = g_sleHostHdiUtOpts->getHostAddress(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleGetHostAddressHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleGetHostAddressHdiTestLevel1 pass");

    ret = g_sleHostHdiUtOpts->getHostAddress(&addr);
    SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleGetHostAddressHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleGetHostAddressHdiTestLevel1 pass");
}

void SleSetHostNameHdiTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    uint8_t name[32] = {0};
    ret = g_sleHostHdiUtOpts->setHostName(name, SLE_DEVICE_NAME_LEN + 1);
    SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleSetHostNameHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleSetHostNameHdiTestLevel1 pass");

    ret = g_sleHostHdiUtOpts->setHostName(hostName, SLE_DEVICE_NAME_LEN);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleSetHostNameHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleSetHostNameHdiTestLevel1 pass");
}

void SleGetHostNameHdiTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    uint8_t name[SLE_DEVICE_NAME_LEN] = {0};
    uint8_t len = SLE_DEVICE_NAME_LEN;
    ret = g_sleHostHdiUtOpts->getHostName(name, &len);
    SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleGetHostNameHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleGetHostNameHdiTestLevel1 pass");
}

void SleDisableSleHostHdiTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = g_sleHostHdiUtOpts->disableSle();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleDisableSleHostHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleDisableSleHostHdiTestLevel1 pass");
}

void SleRegisterHdiTestLevel1(void)
{
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    g_sleHostHdiUtCbs->OnSleEnabledCb = OnSleHdiUtEnabled;
    g_sleHostHdiUtCbs->OnSleDisabledCb = OnSleHdiUtDisabled;
    ret = g_sleHostHdiUtOpts->registerHostCbks(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret != SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleRegisterHdiTestLevel1 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleRegisterHdiTestLevel1 pass");
    ret = g_sleHostHdiUtOpts->registerHostCbks(g_sleHostHdiUtCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HDI_ERRCODE_SUCC, "[SleHostHdiTest][FAILED] SleRegisterHdiTestLevel0 failed(%08X)", ret);
    SLE_UT_LOG("[SleHostHdiTest][PASSED] SleRegisterHdiTestLevel0 pass");
}

void SleHostHdiTestLevel1(void)
{
    SleRegisterHdiTestLevel1();
    SleEnableSleHostHdiTestLevel1();
    sleep(1);
    SleSetHostAddressHdiTestLevel1();
    SleSetHostAddressHdiTestLevel1();
    SleGetHostAddressHdiTestLevel1();
    SleSetHostNameHdiTestLevel1();
    SleGetHostNameHdiTestLevel1();
    SleDisableSleHostHdiTestLevel1();
    sleep(1);
}

SleHdiErrorCode InitSleHostHdiTestEntry(void)
{
    g_sleHostHdiUtOpts = (SleHostHdiOpts*)malloc(sizeof(SleHostHdiOpts));
    SLE_UT_CHECK_AND_RETURN_RET(g_sleHostHdiUtOpts != NULL, SLE_HDI_ERRCODE_MALLOC);
    g_sleHostHdiUtCbs = (SleHostHdiCallbacks*)malloc(sizeof(SleHostHdiCallbacks));
    SLE_UT_CHECK_AND_RETURN_RET(g_sleHostHdiUtCbs != NULL, SLE_HDI_ERRCODE_MALLOC);
    g_sleHostHdiUtCbs->OnSleEnabledCb = OnSleHdiUtEnabled;
    g_sleHostHdiUtCbs->OnSleDisabledCb = OnSleHdiUtDisabled;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode DeinitSleHostHdiTestEntry(void)
{
    free(g_sleHostHdiUtOpts);
    free(g_sleHostHdiUtCbs);
    return SLE_HDI_ERRCODE_SUCC;
}

void SleHostHdiTestEntry(void)
{
    SLE_UT_LOG("__SleHostHdiTestEntry start");
    SleHdiErrorCode ret = SLE_HDI_ERRCODE_FAIL;
    ret = InitSleHostHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_HDI_ERRCODE_SUCC, "InitSleHostHdiTestEntry");
    SleHostHdiTestLevel0();
    // SleHostHdiTestLevel1();
    ret = DeinitSleHostHdiTestEntry();
    SLE_UT_CHECK_AND_RETURN_LOG(ret == SLE_HDI_ERRCODE_SUCC, "DeinitSleHostHdiTestEntry");
    SLE_UT_LOG("__SleHostHdiTestEntry end");
}
